package com.tools.web.interceptor.auth.custom;

import com.tools.common.object.Note;
import com.tools.common.object.json.JsonKit;
import com.tools.common.security.secret.TwoWayCypher;
import com.tools.common.thread.Timeunit;
import com.tools.web.interceptor.auth.GenericAuthToken;
import com.tools.web.interceptor.auth.LoginDeviceType;

import java.util.HashMap;
import java.util.Map;

/**
 * 用 “JSON + 双向加解密” 实现的 AuthToken
 * */
@Note("用 “JSON + 双向加解密” 实现的 AuthToken")
public final class JSONAuthToken extends GenericAuthToken {

    @Note("双向加解密工具实例")
    private final TwoWayCypher cypher;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public JSONAuthToken(TwoWayCypher cypher) {
        if (cypher == null) throw new NullPointerException("JSONAuthToken 的加密工具实例不能为 null");
        this.cypher = cypher;
    }

    public JSONAuthToken(TwoWayCypher cypher, String encryptedToken) {
        this(cypher);
        this.decrypt(encryptedToken);
    }

    /* **************************************************************************************
     *
     *          实现的功能方法
     *
     * **************************************************************************************
     * */

    @Override
    public String encrypt() {
        super.protected_checkExpiration();
        String json = JsonKit.toJSON(this);
        return this.cypher.encrypt(json);
    }


    @Override
    public void decrypt(String encryptedToken) {
        String json = this.cypher.decrypt(encryptedToken);
        Map dataBody = JsonKit.toEntity(json, HashMap.class);
        super.setUserId(dataBody.get("userId"));
        super.setUuid((String) dataBody.get("uuid"));
        super.setLoginDeviceType((String) dataBody.get("loginDeviceType"));
        super.setLoginUserType((String) dataBody.get("loginUserType"));
        super.setLoginTimeMill((long) dataBody.get("loginTime"), Timeunit.MILL);
        super.setExpirationMill((long) dataBody.get("expiration"), Timeunit.MILL);
        super.setExtendedCount((int) dataBody.get("extendedCount"));
    }

    /* **************************************************************************************
     *
     *          链式调用
     *
     * **************************************************************************************
     * */


    @Override
    public JSONAuthToken uuid(String uuid) {
        super.uuid(uuid);
        return this;
    }

    @Override
    public JSONAuthToken userId(Object userId) {
        super.userId(userId);
        return this;
    }

    @Override
    public JSONAuthToken userId(String userId) {
        super.userId(userId);
        return this;
    }

    @Override
    public JSONAuthToken userId(long userId) {
        super.userId(userId);
        return this;
    }

    @Override
    public JSONAuthToken userId(int userId) {
        super.userId(userId);
        return this;
    }

    @Override
    public JSONAuthToken loginDeviceType(String loginDeviceType) {
        super.loginDeviceType(loginDeviceType);
        return this;
    }

    @Override
    public JSONAuthToken loginDeviceType(LoginDeviceType loginDeviceType) {
        super.loginDeviceType(loginDeviceType);
        return this;
    }

    @Override
    public JSONAuthToken loginUserType(String loginUserType) {
        super.loginUserType(loginUserType);
        return this;
    }

    @Override
    public JSONAuthToken loginTimeMill(long loginTime, Timeunit loginTimeunit) {
        super.loginTimeMill(loginTime, loginTimeunit);
        return this;
    }

    @Override
    public JSONAuthToken expirationMill(long expiration, Timeunit expirationTimeunit) {
        super.expirationMill(expiration, expirationTimeunit);
        return this;
    }

    @Override
    public JSONAuthToken extendedCount(int extendedCount) {
        super.extendedCount(extendedCount);
        return this;
    }
}
